package com.xhui.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhui.reggie.dto.DishDTO;
import com.xhui.reggie.entity.Dish;
import com.xhui.reggie.entity.DishFlavor;
import com.xhui.reggie.mapper.DishMapper;
import com.xhui.reggie.service.CategoryService;
import com.xhui.reggie.service.DishFlavorService;
import com.xhui.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private DishFlavorService dishFlavorService; // 操作菜品口味表 dish_Flavor

    @Resource
    private CategoryService categoryService; // 操作 菜品/套餐 分类表 category

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDTO 封装对象
     */
    @Transactional // 多表操作，需要进行事务处理，别忘了在启动类中加上开启注解
    @Override
    public void saveWithFlavor(DishDTO dishDTO) {
        //1. 保存菜品的基本信息 ==> dish表
        this.save(dishDTO);

        Long dishId = dishDTO.getId(); // 获取菜品id
        //2. 保存菜品 口味信息 ==> dish_flavor表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        // 从flavors中获取每个item，然后封装上dishId，生成一个新的List集合
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        // 3. 保存菜品的口味信息
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据 菜品id 查询菜品信息和口味信息
     * @param dishId dish id
     * @return 菜品信息和口味信息
     */
    @Override
    public DishDTO getByIdWithFlavor(Long dishId) {
        // 1.查询菜品基本信息 from dish table
        Dish dish = this.getById(dishId);

        DishDTO dishDTO = new DishDTO();

        // 将dish的属性复制给dishDTO
        BeanUtils.copyProperties(dish, dishDTO);
        //2. 查询菜品口味信息 from dish_flavor table
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishId);
        List<DishFlavor> flavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        // dishDTO设置菜品多个口味信息
        dishDTO.setFlavors(flavors);
        return dishDTO;
    }

    /**
     * 更新指定菜品的信息
     * @param dishDTO 前端菜品更新信息
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {
    // 修改名称，分类, 价格，图片，描述 --> dish
    this.updateById(dishDTO);

    // 修改口味 -> dish_flavor
    // 1.先删除原先的菜品口味
    LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
    dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishDTO.getId());

    dishFlavorService.remove(dishFlavorLambdaQueryWrapper);

    // 2.再添加修改后的菜品口味
    List<DishFlavor> flavors = dishDTO.getFlavors();
    // 2.1 注意此处的flavors是没有菜品id的，因为进行封装前端数据时，是封装不上的
    flavors.stream().forEach(item -> {
        item.setDishId(dishDTO.getId());
    });
    dishFlavorService.saveBatch(flavors);
    //dishFlavorService.updateBatchById(flavors);
    }

    @Override
    @Transactional
    public void updateStatus(int status, String ids) {

        String[] separatedIds = ids.split(",");
        List<Dish> dishList = new ArrayList<>();

        for (int i = 0; i < separatedIds.length; i++) {
            // 将字符串 —-> Long
            Long dishId = Long.valueOf(separatedIds[i]);

            Dish dish = new Dish();
            dish.setId(dishId);
            dish.setStatus(status);

            dishList.add(dish);
        }
        super.updateBatchById(dishList);
    }
}
